﻿
using HualongExcel.Enum;
using HualongExcel.Extensions;
using HualongExcel.Froms;
using HualongExcel.Utilities;
using Microsoft.Office.Tools.Ribbon;
using System;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace HualongExcel
{
    public partial class HualongRibbon
    {

        private void tablePreviewButton_Click(object sender, RibbonControlEventArgs e)
        {
            if (String.IsNullOrEmpty(Common.WorktemplatePath) || Common.WorktemplateData == null)
            {
                MessageBox.Show($"工作模板未设置\n无法预览表格");
                return;
            }
            TablePreviewWindow.OpenTablePreviewWindow();
            //Excelapp.Range["A3:F6, B1:C5"].Select();
        }
        private void HualongRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            var index = (int)SaveHelper.GetData(RibbonConstant.ReplaceTypeIndex.ToString(), 0);
            replaceTypeComboBox.SetIndex(index);
            Common.SelectedReplaceType = (ReplaceType)index;
        }

        private void GenerateTableButton_Click(object sender, RibbonControlEventArgs e)
        {
            if (String.IsNullOrEmpty(Common.WorktemplatePath) || Common.WorktemplateData == null)
            {
                MessageBox.Show($"工作模板未设置");
                return;
            }
            if(Common.SelectedRowCount > 1||Common.SelectedColumnCount>1)
                GenerateSheetsWithTemplate();
             else
                GenerateSheetWithTemplate();
        }



        private void GenerateSheetWithTemplate()
        {
            Common.GenerateSheetWithTemplate(Common.WorktemplateData);
        }
        private void GenerateSheetsWithTemplate()
        {
            Common.GenerateSheetsWithTemplate(Common.WorktemplateData);
        }

        //private void GenerateSheetWithTemplateInMultiThread()
        //{
            //ThreadStart generateTheradStarter = new ThreadStart(GenerateSheetWithTemplate);
            //Thread generateThread = new Thread(generateTheradStarter);
            //generateThread.Start();
        //}



        private void templateConfigButton_Click(object sender, RibbonControlEventArgs e)
        {

            TemplateForm.ShowSignleForm(false);
        }

        private void SetWorktemplateButton_Click(object sender, RibbonControlEventArgs e)
        {
            WorktemplateForm.ShowSignleForm(true);
        }

        private void replaceTypeComboBox_TextChanged(object sender, RibbonControlEventArgs e)
        {
            var index = replaceTypeComboBox.GetCurrentIndex();
            SaveHelper.SetData(RibbonConstant.ReplaceTypeIndex.ToString(), index);
            Common.SelectedReplaceType = (ReplaceType)index;
        }

        private void VariableWindowButton_Click(object sender, RibbonControlEventArgs e)
        {
            VariableWindow.OpenVariableWindow();
        }

        private void AboutButton_Click(object sender, RibbonControlEventArgs e)
        {
            AboutForm.ShowSignleForm(false);
        }

        private void printButton_Click(object sender, RibbonControlEventArgs e)
        {
            if (String.IsNullOrEmpty(Common.WorktemplatePath) || Common.WorktemplateData == null)
            {
                MessageBox.Show($"工作模板未设置\n无法打印表格");
                return;
            }

            GenerateTableButton_Click(sender,e);

            var settings=PrinterHelper.GetPrinterBySelect();
            if (settings != null)
            {
                Common.PrintSheetsWithTemplate(settings);
            }
            //System.Drawing.Printing.PrintDocument pd = new //System.Drawing.Printing.PrintDocument();
            //pd.PrinterSettings.PrinterName = "Hewlett-Packard HP LaserJet Pro MFP M126a";

        }
    }
}
